package com.hd.tiyu.yuejian.Activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hd.tiyu.yuejian.Fragment.ClubClassFragment;
import com.hd.tiyu.yuejian.Fragment.ClubIntroFragment;
import com.hd.tiyu.yuejian.Fragment.ClubVideoFragement;
import com.hd.tiyu.yuejian.JavaBean.ClubIntro;
import com.hd.tiyu.yuejian.Model.ListsModel;
import com.hd.tiyu.yuejian.R;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


//俱乐部详情
public class ClubDetailActivity extends FragmentActivity implements View.OnClickListener {
    private TextView club_name;
    private ImageView club_img,bt_back,guanzhu;

    private LinearLayout jianjie;
    private LinearLayout kehceng;
    private LinearLayout shipin;

    private TextView tex_intro;
    private TextView tex_class;
    private TextView tex_video;

    private Fragment tab_intro;
    private Fragment tab_class;
    private Fragment tab_video;
    private ClubIntro data;
    private boolean flag;

    private SharedPreferences sp;
    private String userId;//为用户id
    private String clubId;
    private String sessionId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        initViews();
        initEvents();
        initData();
        tabListener();
    }

    private void tabListener() {
       jianjie.setOnClickListener(this);
        kehceng.setOnClickListener(this);
        shipin.setOnClickListener(this);
    }

    private void initViews() {
        club_name=(TextView) findViewById(R.id.text_clubname);
        club_img=(ImageView) findViewById(R.id.imageView);
        bt_back=(ImageView) findViewById(R.id.img_clubback);
        guanzhu=(ImageView) findViewById(R.id.img_guanzhu);

        jianjie=(LinearLayout)findViewById(R.id.tab_detail);
        kehceng=(LinearLayout)findViewById(R.id.tab_cl);
        shipin=(LinearLayout)findViewById(R.id.tab_vo);

        tex_intro=(TextView)findViewById(R.id.text_detail);
        tex_class=(TextView)findViewById(R.id.text_cl);
        tex_video=(TextView)findViewById(R.id.text_vo);

        sp = getSharedPreferences("loginUser", Context.MODE_PRIVATE);
        clubId=getIntent().getStringExtra("Club_Id");
        userId=sp.getString("user_id","");
        sessionId=sp.getString("SessionID","");

        ListsModel listsModel=new ListsModel();
        Call<Integer> call=listsModel.getClubState(clubId,userId,"1",sessionId);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                int res=response.body();
                if (res==0){
                    Toast.makeText(ClubDetailActivity.this,res+"",Toast.LENGTH_SHORT).show();
                    flag=true;
                    guanzhu.setImageResource(R.drawable.hong_guanzhu);
                }else {
                    Toast.makeText(ClubDetailActivity.this, res+"", Toast.LENGTH_SHORT).show();
                    flag=false;
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }

    private void initEvents() {
        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ListsModel listsModel=new ListsModel();
                if (!flag){
                    Call<String> call=listsModel.guanzhu(sessionId,userId,clubId,"1");
                    call.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            String res=response.body();
                            if(Integer.parseInt(res)>0){
                                flag=true;
                                guanzhu.setImageResource(R.drawable.hong_guanzhu);
                                Toast.makeText(ClubDetailActivity.this,"关注成功"+ res, Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {

                        }
                    });
                }else {
                    Call<String> call=listsModel.delete("clubfans",clubId,userId);
                    call.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            String res=response.body();
                            if (Integer.parseInt(res)>0){
                                flag=false;
                                guanzhu.setImageResource(R.drawable.bai_guanzhu);
                                Toast.makeText(ClubDetailActivity.this,"取消关注"+res,Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {

                        }
                    });
                }

            }
        });


    }


    private void initData() {
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
        ListsModel listsModel=new ListsModel();
        Call<ClubIntro> call=listsModel.getClubIntro(id);
        call.enqueue(new Callback<ClubIntro>() {
            @Override
            public void onResponse(Call<ClubIntro> call, Response<ClubIntro> response) {
                data=response.body();
                club_name.setText(data.getClub_name());
                Picasso.with(ClubDetailActivity.this)
                        .load("http://172.24.10.175/workout/Uploads/"+data.getThumb())
                        .resize(350,150).centerCrop()
                        .into(club_img);
                setSelect(0);
            }

            @Override
            public void onFailure(Call<ClubIntro> call, Throwable t) {

            }
        });



    }
    @TargetApi(Build.VERSION_CODES.M)
    private void setSelect(int i){
        Bundle bundle=new Bundle();
        bundle.putString("id",data.getId());
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if(tab_intro==null){
                    tab_intro=new ClubIntroFragment();
                    tab_intro.setArguments(bundle);
                    transaction.add(R.id.detailframe,tab_intro);
                }else {
                    transaction.show(tab_intro);
                }
                tex_intro.setTextColor(getColor(R.color.lv));
                break;
            case 1:
                if(tab_class==null){
                    tab_class=new ClubClassFragment();
                    tab_class.setArguments(bundle);
                    transaction.add(R.id.detailframe,tab_class);
                }else {
                    transaction.show(tab_class);
                }
               tex_class.setTextColor(getColor(R.color.lv));
                break;
            case 2:
                if(tab_video==null){
                    tab_video=new ClubVideoFragement();
                    tab_video.setArguments(bundle);
                    transaction.add(R.id.detailframe,tab_video);
                }else {
                    transaction.show(tab_video);
                }
                tex_video.setTextColor(getColor(R.color.lv));
                break;
        }
        transaction.commit();
    }
    private void hideFragment(FragmentTransaction transaction){
        if(tab_intro!=null){
            transaction.hide(tab_intro);
        }
        if(tab_class!=null){
            transaction.hide(tab_class);
        }
        if(tab_video!=null){
            transaction.hide(tab_video);
        }
    }
    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_detail:
                setSelect(0);
                break;
            case R.id.tab_cl:
                setSelect(1);
                break;
            case R.id.tab_vo:
                setSelect(2);
                break;
        }

    }
    @TargetApi(Build.VERSION_CODES.M)
    private void resetImgs() {
        tex_intro.setTextColor(getColor(R.color.bai));
        tex_class.setTextColor(getColor(R.color.bai));
        tex_video.setTextColor(getColor(R.color.bai));
    }


}
